package com.example.project.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.project.entity.IssueCommentAttachment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 需求评论附件Mapper接口
 */
@Mapper
public interface IssueCommentAttachmentMapper {

    /**
     * 根据评论ID获取附件列表
     */
    @Select("SELECT * FROM t_issue_comment_attachment WHERE comment_id = #{commentId}")
    List<IssueCommentAttachment> getAttachmentsByCommentId(@Param("commentId") Integer commentId);

    /**
     * 根据需求ID获取所有评论附件列表
     */
    @Select("SELECT a.* FROM t_issue_comment_attachment a " +
            "JOIN t_issue_comment c ON a.comment_id = c.id " +
            "WHERE c.issue_id = #{issueId}")
    List<IssueCommentAttachment> getAttachmentsByIssueId(@Param("issueId") Integer issueId);

    /**
     * 根据用户ID获取上传的附件列表
     */
    @Select("SELECT a.* FROM t_issue_comment_attachment a " +
            "JOIN t_issue_comment c ON a.comment_id = c.id " +
            "WHERE c.author_id = #{authorId}")
    List<IssueCommentAttachment> getAttachmentsByAuthorId(@Param("authorId") Integer authorId);

    /**
     * 根据文件名查找附件
     */
    @Select("SELECT * FROM t_issue_comment_attachment WHERE filename = #{filename}")
    IssueCommentAttachment getAttachmentByFilename(@Param("filename") String filename);

    /**
     * 获取评论的附件数量
     */
    @Select("SELECT COUNT(*) FROM t_issue_comment_attachment WHERE comment_id = #{commentId}")
    Integer countAttachmentsByCommentId(@Param("commentId") Integer commentId);

    /**
     * 获取需求的附件总数量
     */
    @Select("SELECT COUNT(*) FROM t_issue_comment_attachment a " +
            "JOIN t_issue_comment c ON a.comment_id = c.id " +
            "WHERE c.issue_id = #{issueId}")
    Integer countAttachmentsByIssueId(@Param("issueId") Integer issueId);

    /**
     * 更新附件状态（如删除标记）
     */
    @Update("UPDATE t_issue_comment_attachment SET deleted = #{deleted} WHERE id = #{attachmentId}")
    int updateAttachmentStatus(@Param("attachmentId") Integer attachmentId, @Param("deleted") Boolean deleted);

    /**
     * 根据文件扩展名获取附件列表
     */
    @Select("SELECT * FROM t_issue_comment_attachment WHERE filename LIKE CONCAT('%', '.', #{extension})")
    List<IssueCommentAttachment> getAttachmentsByExtension(@Param("extension") String extension);

    /**
     * 获取大文件附件列表（大于指定大小）
     */
    @Select("SELECT * FROM t_issue_comment_attachment WHERE filesize > #{sizeLimit}")
    List<IssueCommentAttachment> getLargeFiles(@Param("sizeLimit") Long sizeLimit);

    /**
     * 获取最近上传的附件列表
     */
    @Select("SELECT a.* FROM t_issue_comment_attachment a " +
            "JOIN t_issue_comment c ON a.comment_id = c.id " +
            "ORDER BY c.create_time DESC LIMIT #{limit}")
    List<IssueCommentAttachment> getLatestAttachments(@Param("limit") Integer limit);
}